/**
 * 
 */
package controller;

import java.util.Observable;
import java.util.Observer;

import model.ApplicationMessage;
import model.LinkParsingData;

import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.ToolItem;

import core.DownloadManager;
import view.AddLinkDialogView;
import view.ApplicationView;
import view.BaseView;

/**
 * @author Ken
 *
 */
public class ApplicationController extends BaseController implements Observer {
	public ApplicationController(BaseView view) {
		super(view);
		final ApplicationView aView = (ApplicationView)view;
		ToolItem item = aView.toolbar.getItem(0);
		item.addSelectionListener(new AddURLSelectionListener(aView));
		MenuItem taskHeader = aView.mainMenu.getItem(0);
		Menu taskMenu = taskHeader.getMenu();
		taskMenu.getItem(0).addSelectionListener(new AddURLSelectionListener(aView));
		taskMenu.getItem(1).addSelectionListener(new SelectionListener() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				aView.shell.close();
			}			
			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {}
		});
	}
	
	void startDownload(String link) {
		DownloadManager.GetInstance().addObserver(this);
		DownloadManager.GetInstance().GetLinkInfo(link);
	}

	@Override
	public void update(Observable ob, Object data) {
		ApplicationMessage msg = (ApplicationMessage)data;
		switch (msg.type) {
		case URLData:
			ob.deleteObserver(this);
			LinkParsingData linkInfo = (LinkParsingData)data;
			System.out.println("Link = " + linkInfo.link);
			System.out.println("File = " + linkInfo.fileName);
			System.out.println("Protocol = " + linkInfo.protocol);
			System.out.println("Port = " + linkInfo.port);
			System.out.println("Default Port = " + linkInfo.defaultPort);
			System.out.println("Size = " + linkInfo.size);
			DownloadManager.GetInstance().Download(linkInfo.link);
			break;
		default:
			break;
		}
	}
	
	class AddURLSelectionListener implements SelectionListener {
		public AddURLSelectionListener(ApplicationView view) {
			this.view = view;
		}
		
		@Override
		public void widgetDefaultSelected(SelectionEvent event) { }

		@Override
		public void widgetSelected(SelectionEvent event) {
			final AddLinkDialogView addLink = view.createAddURLDialog();
			addLink.useAuthoBtn.addSelectionListener(new SelectionListener() {					
				@Override
				public void widgetSelected(SelectionEvent event) {
					if (addLink.useAuthoBtn.getSelection())
					{
						for (Control control : addLink.authoGroupBox.getChildren()) {
							control.setEnabled(true);
						}
					}
					else {
						for (Control control : addLink.authoGroupBox.getChildren()) {
							control.setEnabled(false);
						}
					}
				}
				@Override
				public void widgetDefaultSelected(SelectionEvent event) { }
			});
			
			addLink.acceptBtn.addSelectionListener(new SelectionListener() {					
				@Override
				public void widgetSelected(SelectionEvent arg0) {
					startDownload(addLink.addressInput.getText());
					addLink.shell.close();
				}
				@Override
				public void widgetDefaultSelected(SelectionEvent arg0) { }
			});
			
			addLink.cancelBtn.addSelectionListener(new SelectionListener() {
				@Override
				public void widgetSelected(SelectionEvent arg0) {
					addLink.shell.close();
				}					
				@Override
				public void widgetDefaultSelected(SelectionEvent arg0) { }
			});
		}
		ApplicationView view;
	}

}